package com.songlanyun.modules.rights.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.exception.OrderException;
import com.songlanyun.modules.rights.entity.RightsOrder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.rights.dao.RightsChildOrderDao;
import com.songlanyun.modules.rights.entity.RightsChildOrder;
import com.songlanyun.modules.rights.service.RightsChildOrderService;


@Service("rightsChildOrderService")
public class RightsChildOrderServiceImpl extends ServiceImpl<RightsChildOrderDao, RightsChildOrder> implements RightsChildOrderService {

    @Override
    public RightsChildOrder create(RightsChildOrder entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public RightsChildOrder modify(RightsChildOrder entity) {
        ValidatorUtils.validateEntity(entity);
        this.baseMapper.updateById(entity);
        return entity;
    }

    @Override
    public void remove(Long id) {

    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RightsChildOrder> page = this.page(
                new Query<RightsChildOrder>().getPage(params),
                new QueryWrapper<RightsChildOrder>()
        );

        return new PageUtils(page);
    }

    @Override
    public RightsChildOrder loadChildRightsOrder(Long rightsOrderId, Long orderId, Long childOrderId, boolean tw) {
        RightsChildOrder one = this.getOne(new LambdaQueryWrapper<RightsChildOrder>()
                .eq(RightsChildOrder::getRightsId, rightsOrderId)
                .eq(RightsChildOrder::getOrderId, orderId)
                .eq(RightsChildOrder::getChildOrderId, childOrderId)
                .last("limit 1"));
        if(one==null && tw)
        {
            throw new RRException(OrderException.RIGHTS_CHILD_ORDER_NOT_EXISTS);
        }

        return one;
    }

    @Override
    public RightsChildOrder loadChildRightsOrder(Long rightsOrderId, Long rightsChildOrderId, boolean tw) {
        RightsChildOrder one = this.getOne(new LambdaQueryWrapper<RightsChildOrder>()
                .eq(RightsChildOrder::getRightsId, rightsOrderId)
                .eq(RightsChildOrder::getId, rightsChildOrderId)
                .last("limit 1"));
        if(one==null && tw)
        {
            throw new RRException(OrderException.RIGHTS_CHILD_ORDER_NOT_EXISTS);
        }

        return one;
    }

    @Override
    public List<RightsChildOrder> loadChildRightsOrders(Long rightsOrderId) {
        return this.list(new LambdaQueryWrapper<RightsChildOrder>()
                .eq(RightsChildOrder::getRightsId, rightsOrderId));
    }


}